/**
 * @class
 * @name Model.Grid
 * @extends Model
 */
Model.Grid = Model.$extend({

    /**
     * @private
     * @field
     * @name Model.Grid#selectQuery
     * @see Model.Grid#execSelect
     * @type {String|null}
     */
    selectQuery : null,

    /**
     * @private
     * @field
     * @name Model.Grid#deleteQuery
     * @see Model.Grid#execDelete
     * @type {String|null}
     */
    deleteQuery : null,

    /**
     * @private
     * @field
     * @name Model.Grid#updateQuery
     * @see Model.Grid#execUpdate
     * @type {String|null}
     */
    updateQuery : null,

    /**
     * @private
     * @field
     * @name Model.Grid#insertQuery
     * @see Model.Grid#execInsert
     * @type {String|null}
     */
    insertQuery : null,

    /**
     * @private
     * @field
     * @name Model.Grid#connection
     * @see Model.Grid#getConnection
     * @type {*|null}
     */
    connection : null,

    /**
     * @public
     * @function
     * @name Model.Grid#$init
     * @param {Object} options
     * @param {Function} callback
     * @return {void}
     */
    $init : function (options, callback) {
        var self = this;
        options = options || {};
        if (typeof options['selectQuery'] !== 'string') {
            throw new TypeError('Не задан запрос чтения данных.');
        }
        if (typeof options['deleteQuery'] !== 'string') {
            throw new TypeError('Не задан запрос удаления данных.');
        }
        if (typeof options['updateQuery'] !== 'string') {
            throw new TypeError('Не задан запрос обновления данных.');
        }
        if (typeof options['insertQuery'] !== 'string') {
            throw new TypeError('Не задан запрос вставки данных.');
        }
        if (typeof options['mysqlConnection'] !== 'string') {
            throw new TypeError('Не задан запрос вставки данных.');
        }
        if (typeof callback !== 'function') {
            throw new TypeError('Обработчик соединения с базой данных не задан.');
        }
        this.selectQuery = options['selectQuery'];
        this.deleteQuery = options['deleteQuery'];
        this.updateQuery = options['updateQuery'];
        this.insertQuery = options['insertQuery'];
        Application.getMysqlConnection(function (error, connection) {
            self.connection = connection;

        });
    },

    getConnection : function () {
        return this.connection;
    },

    execSelect : function (callback) {},

    execDelete : function (callback) {},

    execUpdate : function (id, data, callback) {},

    execInsert : function (data, callback) {
        var query = this.insertQuery;
        data = data || {};
        query = query.replace(/#\{([a-z_]\w*)\}/g, function (matches) {
            // Название переменной шаблона.
            var name = String(matches[1] || '');
            // Заменяем переменную в запросе.
            return typeof data[name] === 'string' ? data[name] : '';
        });

    }


});
